﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LadderController : MonoBehaviour
{
    public float climbSpeed;
    private bool inLadder;
    public Collider2D ladderColl;

    private bool up = false, down = false, space = false;

    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            up = true;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            down = true;
        }
        else if (Input.GetKey(KeyCode.Space))
        {
            space = true;
        }

        else
        {
            up = false;
            down = false;
            space = false;
        }
    }

    private void OnTriggerEnter2D(Collider2D coll)
    {
        if (coll.tag == "Player")
        {
            Debug.Log(1);
            if (up)
            {
                Debug.Log(1);
                GameObject.FindWithTag("Player").SendMessage("setLadder", true);//setLadder控制攀爬动画
                GameObject.FindWithTag("Player").SendMessage("setInLadder", false);//setInLadder控制停留动画
                inLadder = true;//用于确保停留动画正常
                coll.GetComponent<Rigidbody2D>().gravityScale = 0;
                //在梯子上时忽略player和所到平台的碰撞
                Physics2D.IgnoreCollision(coll.GetComponents<Collider2D>()[0], ladderColl);
                Physics2D.IgnoreCollision(coll.GetComponents<Collider2D>()[1], ladderColl);
                coll.GetComponent<Rigidbody2D>().velocity = new Vector2(0, climbSpeed);
            }

            else if (down)
            {
                Debug.Log(2);
                GameObject.FindWithTag("Player").SendMessage("setLadder", true);
                GameObject.FindWithTag("Player").SendMessage("setInLadder", false);
                inLadder = true;
                coll.GetComponent<Rigidbody2D>().gravityScale = 0;
                Physics2D.IgnoreCollision(coll.GetComponents<Collider2D>()[0], ladderColl);
                Physics2D.IgnoreCollision(coll.GetComponents<Collider2D>()[1], ladderColl);
                coll.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -climbSpeed);
            }
            else if (space)
            {
                Debug.Log(3);
                GameObject.FindWithTag("Player").SendMessage("setLadder", false);
                GameObject.FindWithTag("Player").SendMessage("setInLadder", true);
                Physics2D.IgnoreCollision(coll.GetComponents<Collider2D>()[0], ladderColl, false);
                Physics2D.IgnoreCollision(coll.GetComponents<Collider2D>()[1], ladderColl, false);
                coll.GetComponent<Rigidbody2D>().gravityScale = 3;
                inLadder = false;
            }
            else if (inLadder)
            {
                Debug.Log(4);
                coll.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
                coll.SendMessage("setLadder", false);
                coll.SendMessage("setInLadder", true);
            }
        }
    }
    private void OnTriggerExit2D(Collider2D coll)
    {
        GameObject.FindWithTag("Player").SendMessage("setLadder", false);
        GameObject.FindWithTag("Player").SendMessage("setInLadder", false);
        coll.GetComponent<Rigidbody2D>().gravityScale = 3;
        inLadder = false;
        Physics2D.IgnoreCollision(coll.GetComponents<Collider2D>()[0], ladderColl, false);
        Physics2D.IgnoreCollision(coll.GetComponents<Collider2D>()[0], ladderColl, false);
    }
}
